<?php
require_once 'conexao.php';
?>
<html>
    <body>
        <table align="left" border="1" cellpadding="1" cellspacing="1" style="width: 1210px; height: 93px;">

            <tr>
                <td style="width: 200px; height: 70px;">
                    <img alt="" src="http://www.saoexpedito.com.br/imagens/logo.jpg" style="width: 201px; height: 70px;" /></td>
                <td style="text-align: center;"><h1>CONTROLE DE DIÁRIAS</h1></td>
                <td style="width: 150px;">
                    Rev.: 00<br />
                    Data: 29/11/2011<br />
                    Página 1 de 1</td>
            </tr>

        </table> 
        <br /><br /><br /><br /><br /><br />
        <?php
        $motorista = $_POST['motorista'];

        $datas = explode('/', $_POST['dt_entrada_ini']);
        $datade = $datas['2'] . "-" . $datas['1'] . "-" . $datas['0'];
        $datas = explode('/', $_POST['dt_entrada_fim']);
        $dataate = $datas['2'] . "-" . $datas['1'] . "-" . $datas['0'];
        ?>
        <table align='left' border='1' cellpadding='1' cellspacing='1' style='width: 1210px;'>
            <tr>
                <td>Motoristas \ Datas</td>
                <?php
                $dt_ent = $datade;
                while ($dt_ent <= $dataate) {
                    echo "<td>'$dt_ent'</td>";
                    $dt_ent = date('Y-m-d', strtotime("+1 days", strtotime($dt_ent)));
                }
                ?>
                <td>Total</td>
            </tr>
            <?php
            $sql_mot = "SELECT nome FROM motorista WHERE bloqueado = 0";
            if ($motorista != '*')
                $sql_mot = $sql_mot . " AND nome='" . $motorista . "'";
            $relstat_mot = mysql_query($sql_mot);
            echo mysql_error();

            while ($vet_mot = mysql_fetch_array($relstat_mot)) {
                $motorista = $vet_mot['nome'];

                $query = "SELECT id,dt_entrada, dt_saida, mot_entrada, mot_saida, viagem FROM frota WHERE ((dt_entrada >='$datade' AND dt_entrada<='$dataate') OR (dt_saida >='$datade' AND dt_saida<='$dataate'))";
                $query = $query . " AND (mot_entrada = '$motorista' OR mot_saida = '$motorista')";


                $result = mysql_query($query);

                echo "<tr>";
                echo "<td>" . $motorista . "</td>";

                $i = 0;
                $diaria = 0;
                if (mysql_num_rows($result) == 0)
                    $dt_ent = $datade;
                While ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
                    $i++;
                    echo $row['id'];
                    if ($row['mot_saida'] == $motorista and $row['viagem'] == 1 and $i < mysql_num_rows($result)) {
                        $dt_saida = $row['dt_saida'];
                        $vet_ent = mysql_fetch_array($result);
                        $dt_ent = $vet_ent['dt_entrada'];
                        while ($dt_saida < $dt_ent) {
                            echo "<td>" . $dt_saida . " " . $diaria . "</td>";
                            $diaria++;
                            $dt_saida = date('Y-m-d', strtotime("+1 days", strtotime($dt_saida)));
                        }
                        $branco = 0;
                        while ($sem_viagem = mysql_fetch_array($result) and $branco == 0) {
                            if ($sem_viagem['viagem'] == 1) {
                                $branco = 1;
                                do {
                                    echo "<td style='background: #D3D3D3'>" . $dt_ent . " " . $sem_viagem['dt_saida'] . "</td>";
                                    $dt_ent = date('Y-m-d', strtotime("+1 days", strtotime($dt_ent)));
                                } while ($dt_ent < $sem_viagem['dt_saida']);
                            }
                        }
                        if ($i < mysql_num_rows($result)) {
                            mysql_data_seek($result, $i);
                            echo $row['id'] . ", ";
                        }
                    }
                }

                while ($dt_ent <= $dataate) {
                    echo "<td style='background: #D3D3D3'>" . $dt_ent . " " . $dataate . "</td>";
                    $dt_ent = date('Y-m-d', strtotime("+1 days", strtotime($dt_ent)));
                }
                echo "<td>" . $diaria . "</td></tr>";
            }
            ?>

        </table>
    </body>
</html>